package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    //查询品牌功能
    @GetMapping("/page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrand(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "sortBy",defaultValue = "id")String sortBy,
            @RequestParam(value = "desc",defaultValue = "false")Boolean desc){
        return ResponseEntity.ok(brandService.queryBrand(key,page,rows,sortBy,desc));
    }

    //新增品牌
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO,@RequestParam(value = "cids") List<Long> cids){
        brandService.saveBrand(brandDTO,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //根据分类id查询品牌
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCid(@RequestParam("id")Long cid){
        return ResponseEntity.ok(brandService.queryBrandByCid(cid));
    }

    //根据ID查询品牌信息
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandId(@PathVariable("id")Long id){
        return ResponseEntity.ok(brandService.queryBrandId(id));
    }

    //根据id的集合查询商品品牌
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam(value = "ids")List<Long> ids){
        return ResponseEntity.ok(brandService.queryBrandByIds(ids));
    }

}
